package lgkj.meicqt20.ui;

import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import lgkj.meicqt20.R;
import lgkj.meicqt20.adapter.EliteAdapter;
import lgkj.meicqt20.adapter.HairstyleAdapter;
import lgkj.meicqt20.bean.EliteEnity;
import lgkj.meicqt20.bean.HairstyleEntity;
import lgkj.meicqt20.bean.MessageEvent;
import lgkj.meicqt20.utils.AllKindsOfConstant;
import lgkj.meicqt20.utils.CommUtils;
import lgkj.meicqt20.utils.HttpUtils;
import lgkj.meicqt20.view.BannerViewpager;
import lgkj.meicqt20.view.NonScrollGridView;
import lgkj.meicqt20.view.NonScrollListView;

/**
 * Created by Administrator on 2019/1/18 0018.
 */

public class ShopBoutiqueActivity extends BaseActivity {
    @BindView(R.id.mdxy)
    ImageView mdxy;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.video_little_banner)
    BannerViewpager videoLittleBanner;
    @BindView(R.id.yuan_xian_re_ying)
    ImageButton yuanXianReYing;
    @BindView(R.id.tu_dou_shi_pin)
    ImageButton tuDouShiPin;
    @BindView(R.id.yu_le_shi_pin)
    ImageButton yuLeShiPin;
    @BindView(R.id.ibt_women)
    ImageButton ibtWomen;
    @BindView(R.id.ibt_men)
    ImageButton ibtMen;
    @BindView(R.id.ll_hair)
    LinearLayout llHair;
    @BindView(R.id.non_girdview)
    NonScrollGridView nonGirdview;
    @BindView(R.id.non_listview)
    NonScrollListView nonListview;
    @BindView(R.id.iv_price)
    ImageView ivPrice;
    @BindView(R.id.mm)
    ScrollView scrollView;
    private ImageButton[] bt_select = new ImageButton[2];
    private HairstyleEntity hairsdata;
    private EliteAdapter eliteAdapter;  //造型师
    private HairstyleAdapter hairstyleAdapter;  //发型屋
    private View headConView[];
    private ImageButton headViews[];
    private int headPosition;

    @Override
    public int setContentView() {
        return R.layout.activity_shop_boutique;
    }

    @Override
    public void init() {
        activitiesList.add(this);
        displayDifferen();
        bandViewInit(new initBannersDataResult() {
            @Override
            public void resultSuccess(List<String> list) {
                videoLittleBanner.setData(list);
            }
        });
        HttpUtils.getQRCode(ShopBoutiqueActivity.this, ivCode);
        headConView = new View[]{nonListview, ivPrice, nonGirdview};
        headViews = new ImageButton[]{yuanXianReYing, tuDouShiPin, yuLeShiPin};
        bt_select[0] = ibtWomen;
        bt_select[0].setEnabled(false);
        bt_select[1] = ibtMen;
        bt_select[1].setEnabled(false);
        hairstyleAdapter = new HairstyleAdapter(ShopBoutiqueActivity.this);
        nonGirdview.setAdapter(hairstyleAdapter);
        eliteAdapter = new EliteAdapter(ShopBoutiqueActivity.this);
        nonListview.setAdapter(eliteAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        Log.d(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        Log.i(TAG, "socket: " + messageEvent.toString());
        if ("control".equals(messageEvent.getTagName())) {
            fragmentDealOrder(messageEvent.getMessage());
        }
    }


    private void fragmentDealOrder(String order) {
        switch (order) {
            case "up":
                if (style == 2) {
                    style = 1;
                    gvPositon = -1;
                } else if (style == 1) {
                    style = 0;
                    bt_select[0].setEnabled(false);
                    bt_select[1].setEnabled(false);
                } else if (style == 3) {
                    if (lvPosition == 0) {
                        style = 0;
                        lvPosition = -1;
                    } else {
                        lvPosition--;
                    }
                    eliteAdapter.setPostion(lvPosition);
                    eliteAdapter.notifyDataSetChanged();
                    scrollView.scrollBy(0, (int) getResources().getDimension(R.dimen.yPrivice));
                }
                break;
            case "down":
                if (style == 0 && headPosition == 2) {
                    style = 1;
                    bt_select[0].setEnabled(true);
                } else if (style == 1) {
                    style = 2;
                    gvPositon = 0;
                }
                if (headPosition == 0) {
                    style = 3;
                    if ((lvPosition + 1) < eliteAdapter.getCount()) {
                        lvPosition++;
                    }
                    eliteAdapter.setPostion(lvPosition);
                    eliteAdapter.notifyDataSetChanged();
                    scrollView.scrollBy(0, 520);
                }
                break;
            case "left":
                if (style == 0) {
                    if (headPosition > 0) {
                        headPosition--;
                        headEnabled(headPosition);
                    }
                } else if (style == 1) {
                    bt_select[0].setEnabled(true);
                    bt_select[1].setEnabled(false);
                    checkSex("2");
                } else if (style == 2 && (gvPositon - 1) >= 0) {
                    gvPositon--;
                }
                break;
            case "right":
                if (style == 0) {
                    if (headPosition < 2) {
                        headPosition++;
                        headEnabled(headPosition);
                    }
                } else if (style == 1) {
                    bt_select[0].setEnabled(false);
                    bt_select[1].setEnabled(true);
                    checkSex("1");
                } else if (style == 2) {
                    if ((gvPositon + 1) < hairstyleAdapter.getCount()) {
                        gvPositon++;
                    }
                }
                break;
            case "back":
                finish();
                break;
            case "sure":
                break;
        }
        if (hairstyleAdapter.getCount() > 0) {
            hairstyleAdapter.setpositon(gvPositon);
            hairstyleAdapter.notifyDataSetChanged();
        }
        Log.d(TAG, "order:" + order + ",headPosition:" + headPosition + ",style:" + style + ",gvPositon:" + gvPositon + ",lvPosition:" + lvPosition);
    }

    @Override
    public void initData() {
        getElite();
        getFaHose();
        getPrice();
        headEnabled(0);
    }

    private int style = 0;    //0是大标题    1是发型选择   2发型屋图片
    private int gvPositon = 0;
    private int lvPosition = 0;

    private void headEnabled(int mpositon) {
        lvPosition = -1;
        gvPositon = -1;
        for (int i = 0; i < headViews.length; i++) {
            if (i == mpositon) {
                headViews[i].setEnabled(true);
                headConView[i].setVisibility(View.VISIBLE);
            } else {
                headConView[i].setVisibility(View.INVISIBLE);
                headViews[i].setEnabled(false);
            }
        }
        if (mpositon == 2) {
            llHair.setVisibility(View.VISIBLE);
        } else {
            llHair.setVisibility(View.GONE);
        }
        scrollView.scrollBy(0, 0);
    }

    private void checkSex(String sex) {
        gvPositon = -1;
        hairsdatas.clear();
        if (hairsdata != null) {
            for (int i = 0; i < hairsdata.getData().size(); i++) {
                Log.d(TAG, "checkSex: " + hairsdata.getData().get(i).toString());
                if (sex.equals(hairsdata.getData().get(i).getHairstyle_type())) {
                    hairsdatas.add(hairsdata.getData().get(i));
                }
            }
            hairstyleAdapter.setData(hairsdatas);
            hairstyleAdapter.notifyDataSetChanged();
        }
    }

    private List<HairstyleEntity.DataBean> hairsdatas = new ArrayList<>();

    /**
     * 造型师
     */
    private void getElite() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.getHttp(map, AllKindsOfConstant.getbanber, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                EliteEnity eliteEnity;
                if (jsonObject.optInt("status") == 1) {
                    eliteEnity = new Gson().fromJson(jsonObject.toString(), EliteEnity.class);
                } else {
                    eliteEnity = new EliteEnity();
                }
                eliteAdapter.setData(eliteEnity.getData());
                eliteAdapter.notifyDataSetChanged();

            }

            @Override
            public void errorHttp(String msg) {

            }
        });

    }

    /**
     * 价目表
     */
    private void getPrice() {
        Map<String, String> price = new HashMap<>();
        HttpUtils.getHttp(price, AllKindsOfConstant.getprice, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                try {
                    if (jsonObject.optInt("status") == 1) {
                        String barberprice_img_url = jsonObject.getJSONObject("data").getString("barberprice_img_url");
                        Log.d(TAG, barberprice_img_url);
                        CommUtils.LoadImage(ShopBoutiqueActivity.this, ivPrice, barberprice_img_url, R.drawable.common_icon_err);
                    }
                } catch (Exception e) {
                    Log.e(TAG, "successHttp: " + e.getMessage());
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    /**
     * 发型屋
     */
    private void getFaHose() {
        Map<String, String> sexMap = new HashMap<>();
        HttpUtils.getHttp(sexMap, AllKindsOfConstant.FAXINGPATH, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("status") == 1) {
                    Gson gson = new Gson();
                    hairsdata = gson.fromJson(jsonObject.toString(), HairstyleEntity.class);
                    checkSex("2");
                }
            }

            @Override
            public void errorHttp(String msg) {
            }
        });
    }

}
